﻿using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Targets;
using Prism.Ioc;
using System;

namespace LaserAutoTest.UI.Extensions
{
    public static class NLogExtension
    {
        public static void RegisterNLog(this IContainerRegistry containerRegistry)
        {
            var config = new NLog.Config.LoggingConfiguration();
            var infoFile = new FileTarget("infoFile")
            {
                FileName = "${basedir}/Logs/${date:format=yyyyMMdd}/Info-${date:format=yyyy-MM-dd}.txt",
                Layout = "${longdate} | ${level} | ${callsite} | ${message} | ${exception:format=tostring}"
            };

            config.AddTarget(infoFile);

            var errorFile = new FileTarget("errorFile")
            {
                FileName = "${basedir}/Logs/${date:format=yyyyMMdd}/Error-${date:format=yyyy-MM-dd}.txt",
                Layout = "${longdate} | ${level} | ${callsite} | ${message} | ${exception:format=tostring}"
            };

            config.AddTarget(errorFile);

            var debugFile = new FileTarget("debugFile")
            {
                FileName = "${basedir}/Logs/${date:format=yyyyMMdd}/Debug-${date:format=yyyy-MM-dd}.txt",
                Layout = "${longdate} | ${level} | ${callsite} | ${message} | ${exception:format=tostring}"
            };

            config.AddTarget(debugFile);

            config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Info, infoFile);
            config.AddRule(NLog.LogLevel.Error, NLog.LogLevel.Fatal, errorFile);
            config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Debug, debugFile);
            NLog.LogManager.Configuration = config;

            var factory = new NLogLoggerFactory();
            ILogger logger = factory.CreateLogger("LaserAutoTest");

            containerRegistry.RegisterInstance<ILogger>(logger);
        }
    }
}
